/*
 *  $URL$
 *  $Rev$
 *  $Date$
 *  $Id$
 *  Copyright 2009 Interactive Media Management.
 */
package com.interactivemediamanagement.converge.web.dav;

import com.bradmcevoy.common.Path;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.ResourceFactory;
import dk.i2m.converge.domain.Outlet;
import dk.i2m.converge.domain.NewsItem;
import dk.i2m.converge.ejb.facades.NewsItemFacadeLocal;
import dk.i2m.converge.ejb.facades.OutletFacadeLocal;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Allan Lykke Christensen
 */
public class NewsItemResourceFactory implements ResourceFactory {

    private NewsItemFacadeLocal articleFacadeLocal;

    private OutletFacadeLocal outletFacadeLocal;

    public Resource getResource(String host, String url) {
        System.out.println("getResource: url: " + host + url);
        Path path = Path.path(url);
        NewsItemResource res = find(path);
        return res;
    }

    private NewsItemResource find(Path path) {
        if (path == null || path.getParent().isRoot()) {
            NewsItemFolderResource root = new NewsItemFolderResource(null, "localhost:8080/converge-webdav");

            for (Outlet outlet : getOutletFacade().findAllOutlets()) {
                NewsItemFolderResource folder = new NewsItemFolderResource(root, String.valueOf(outlet.getId()) +
                        "-" + outlet.getTitle());
                try {
                    List<NewsItem> articles = getArticleFacade().findByOutlet(0, 50, outlet);

                    for (NewsItem article : articles) {
                        NewsItemResource file = new NewsItemResource(folder, String.valueOf(article.getId()) + ".xml",
                                article);
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
            return root;
        }

        NewsItemResource r = find(path.getParent());
        if (r == null) {
            return null;
        }
        if (r instanceof NewsItemFolderResource) {
            NewsItemFolderResource folder = (NewsItemFolderResource) r;
            for (Resource rChild : folder.getChildren()) {
                NewsItemResource r2 = (NewsItemResource) rChild;
                if (r2.getName().equals(path.getName())) {
                    return r2;
                }
            }
        }
        System.out.println("not found: " + path);
        return null;
    }

    public String getSupportedLevels() {
        return "1,2";
    }

    protected NewsItemFacadeLocal getArticleFacade() {
        if (articleFacadeLocal == null) {
            try {
                Context c = new InitialContext();
                articleFacadeLocal = (NewsItemFacadeLocal) c.lookup("java:comp/env/ArticleFacadeBean");
            } catch (NamingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return articleFacadeLocal;
    }

    protected OutletFacadeLocal getOutletFacade() {
        if (outletFacadeLocal == null) {
            try {
                Context c = new InitialContext();
                outletFacadeLocal = (OutletFacadeLocal) c.lookup("java:comp/env/OutletFacadeBean");
            } catch (NamingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return outletFacadeLocal;
    }
}
